﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebTV_Kickstarter;

namespace Kickstarter.Controllers
{
    public class ProjectController : Controller
    {
        

        // GET: /Project/
        public ActionResult Index()
        {

            IQueryable<Project> projectset = Project.Index();
            if (Session["Gebruiker"] != null)
            {
                Gebruiker g = (Gebruiker) Session["Gebruiker"];
                ViewBag.isAdmin = g.isAdmin;
            }
            else
            {
                ViewBag.isAdmin = false;
            }
            return View(projectset.ToList());
        }

        // GET: /Project/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if (Session["Gebruiker"] != null)
            {
                Gebruiker g = (Gebruiker)Session["Gebruiker"];
                ViewBag.isAdmin = g.isAdmin;
            }
            else
            {
                ViewBag.isAdmin = false;
            }

            Project project = Project.FindProject(id);
            List<Message> message = Message.FindMessages(id);
            Message m = new Message();
            m.Context = "Er zijn nog geen berichten";
            
            if(message.Count() == 0)
                message.Add(m);

            ViewBag.berichten = message;
            

            EFDataModelContainer db = Project.GetTempDbContainer();

            //ViewData["totalDonated"] = new string;
            ViewData["totalDonated"] = project.TotalAmount;
            ViewData["goals"] = new List<string[]>();
            ViewData["goals"] = Goal.FindGoalsForProjectWithDbContainer(project, db);

            if (project == null)
            {
                return HttpNotFound();
            }
            Session["redirectBack"] = "../Project/Details/"+id;
            return View(project);
        }

        // GET: /Project/Create
        public ActionResult Create()
        {
            if(Session["Gebruiker"]  != null)
            { 
            Gebruiker g = (Gebruiker)Session["Gebruiker"];
            ViewBag.UserId = g.Name;
                if (g.isAdmin == false)
                    return RedirectToAction("NietBeheerder", "Home");
            }
            else
                return RedirectToAction("NietBeheerder", "Home");

            
            return View();
        }

        // POST: /Project/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Include = "Title,Description,")] Project project, string goalTitle, string goalDescription, string goalAmount)
        {
            if (Session["Gebruiker"] != null)
            {
                Gebruiker g = (Gebruiker)Session["Gebruiker"];
                ViewBag.UserId = g.Name;
                if (g.isAdmin == false)
                    return RedirectToAction("NietBeheerder", "Home");
            }
            else
                return RedirectToAction("NietBeheerder", "Home");

            if (ModelState.IsValid)
            {
                Gebruiker tempGebruiker = (Gebruiker) Session["Gebruiker"];
                Project.CreateProject(project, tempGebruiker);
                Goal.AddGoalWithDbContainer(Project.CreateGoalForProject(project,  goalTitle,  goalAmount,  goalDescription), Project.GetTempDbContainer());
                return RedirectToAction("Index");
            }
            
            ViewBag.UserId = new SelectList(Project.GetGebruikerSet(), "Id", "Name", project.UserId);
            return View(project);
        }

       

        // GET: /Project/Edit/5
        public ActionResult Edit(int? id)
        {
            if (Session["Gebruiker"] != null)
            {
                Gebruiker g = (Gebruiker)Session["Gebruiker"];
                if (g.isAdmin == false)
                    return RedirectToAction("NietBeheerder", "Home");
            }
            else
                return RedirectToAction("NietBeheerder", "Home");

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Project project = Project.FindProject(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            ViewBag.UserId = new SelectList(Project.GetGebruikerSet(), "Id", "Name", project.UserId);

            return View(project);
        }

        // POST: /Project/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit([Bind(Include = "Id,Title,Description,Goal,Events,TotalAmount,AvgRating,UserId,NumberOfVotes")] Project project)
        {
            if (ModelState.IsValid)
            {
                Project.EditProject(project);
                return RedirectToAction("Index");
            }
            ViewBag.UserId = new SelectList(Project.GetGebruikerSet(), "Id", "Name", project.UserId);
            return View(project);
        }

        // GET: /Project/Delete/5
        public ActionResult Delete(int? id)
        {
            if (Session["Gebruiker"] != null)
            {
                Gebruiker g = (Gebruiker)Session["Gebruiker"];
                if (g.isAdmin == false)
                    return RedirectToAction("NietBeheerder", "Home");
            }
            else
                return RedirectToAction("NietBeheerder", "Home");

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Project project = Project.FindProject(id);
            if (project == null)
            {
                return HttpNotFound();
            }
            return View(project);
        }

        // POST: /Project/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Project.DeleteProject(Project.FindProject(id));
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                Project.Dispose();
            }
            base.Dispose(disposing);
        }

        [HttpGet]
        public String GetReacties(int id)
        {
            Project project = Project.FindProject(id);
            string html = "";
            foreach (Message item in project.Message)
            {
                html += "<dt>" + item.Gebruiker.Name + "</dt><dd>" + item.Context + "</dd>";
            }
            return html;
        }

        [HttpPost]
        public void SetReactie([Bind(Include = "Context, ProjectId")] AjaxReactie m)
        {

            if (Session["Gebruiker"] != null)
            {
                Gebruiker g = (Gebruiker)Session["Gebruiker"];
                Message addedMessage = new Message();
                addedMessage.Context = m.Context;
                addedMessage.Project = Project.FindProject(Convert.ToInt32(m.ProjectId));
                addedMessage.Gebruiker = Gebruiker.FindGebruikerWithDbContainer(g.Id, Project.GetTempDbContainer());
                
                Message.AddMessage(addedMessage, Project.GetTempDbContainer());
            }
          
        }
    }

    public class AjaxReactie
    {
        public string ProjectId { get; set; }
        public string Context { get; set; }
    }
}
